<?php
declare (strict_types = 1);

namespace app\index\controller;


use app\index\BaseController;
use app\index\model\Block;
use think\Exception;
use app\index\model\Article as ArticleModel;
use app\index\model\Comment as CommentModel;
use app\index\model\Like as LikeModel;
use think\facade\Log;

class Article extends BaseController
{
    //中间件
    protected $middleware = [
        'method:ajax' => ['only'=>['list','listByTag']]
    ];


    //文章列表
    public function list(){

        try{

            $where = [];
            //构造查询条件
            $best = input('best/d');
            if ($best) $where[] = ['best','=', $best];
            $category_id = input('category_id/d');
            if ($category_id) $where[] = ['category_id', '=', $category_id];
            $topic_id = input('topic_id/d');
            if ($topic_id) $where[] = ['topic_id','=',$topic_id];
            $title = input('title/s');
            if ($title) $where[] = ['title', 'like', '%'.$title.'%'];
            $user_id = input('user_id/s');
            if ($user_id) $where[] = ['user_id', '=', $user_id];




            //获取参数
            $page = input('page/d');
            $limit= input('limit/d');
            $data = ArticleModel::getList($page, $limit, $where);

            return json([
                'code' => 0,
                'data'  => $data
            ]);
        }catch (\Exception $e){
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg'  => $e->getMessage()
            ]);
        }
    }

    //文章列表
    public function listByTag(){

        try{
            //获取标签ID
            $tag_id = input('tag_id/d');
            if ($tag_id <= 0) throw new Exception('参数错误.');


            //获取参数
            $page = input('page/d');
            $limit= input('limit/d');
            $data = ArticleModel::getListByTag($page, $limit, $tag_id);

            return json([
                'code' => 0,
                'data'  => $data
            ]);
        }catch (\Exception $e){
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg'  => $e->getMessage()
            ]);
        }
    }

    //热门文章
    public function hotList(){

        try{

            //获取参数
            $page = input('page/d');
            $limit= input('limit/d');
            $data = ArticleModel::getHotList($page, $limit);

            return json([
                'code' => 0,
                'data'  => $data
            ]);
        }catch (\Exception $e){
            //Log::error($e->getMessage());
            return json([
                'code' => 1,
                'msg'  => $e->getMessage()
            ]);
        }
    }


    //文章详情
    public function read(int $id){
        $article = $this->getModel(ArticleModel::class, $id,['user','topic','category'],['comments','likes']);
        if ($article->status !== ArticleModel::STATUS_PUB) throw new Exception('页面不存在.');

        $article->save([
            'visited' => (int)$article->visited + 1
        ]);

        //内容和文章分开查询 用户缓存
        $content = $article->getContent();

        //是否收藏
        $likedModel = LikeModel::likedModel((int) $article->id);

        //面包屑导航(如果文章不属于某话题 则不显示话题连接)
        $crumb['crumb']['home'] = '/';
        if ($article->topic_id)
            $crumb['crumb'][$article->topic_name] = (string) url('topic/read',['id'=>$article->topic_id]);
        $crumb['crumb']['文章详情'] = request()->url();
        return view('read',array_merge([
            'article' => $article,
            'content' => $content,
            'prevNext' => $article->getPrevNext(),
            'count' =>CommentModel::getCount($id),
            'limit' =>CommentModel::PAGE_LIMIT_NUM,
            'liked' => $likedModel && $likedModel->isExists(),

            //*热门文章
            'activeArticles' => Block::getActiveArticles(),
            //*推荐话题
            'activeTopics' => Block::getActiveTopics(),

        ],$crumb));
    }
}
